package com.qdsg.ysg.doctor.nurse.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qdsg.ysg.doctor.R;
import com.qdsg.ysg.doctor.base.BaseActivity;
import com.qdsg.ysg.doctor.ui.dialog.AddCallPersonDialog;
import com.qdsg.ysg.doctor.ui.dialog.UpdateDialog;
import com.qdsg.ysg.doctor.util.ToastUtil;
import com.qdsg.ysg.doctor.util.UtilString;
import com.rest.business.RestProxy;
import com.rest.response.BaseResponse;
import com.rest.response.ContactFindVO;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * Description
 * Created by lbj on 2021/4/23 17:50
 */
public class AtyAddCallPerson extends BaseActivity {


    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_name_one)
    TextView tvNameOne;
    @BindView(R.id.tv_phone_one)
    TextView tvPhoneOne;
    @BindView(R.id.tv_delete_one)
    TextView tvDeleteOne;
    @BindView(R.id.ll_call_one)
    LinearLayout llCallOne;
    @BindView(R.id.tv_name_two)
    TextView tvNameTwo;
    @BindView(R.id.tv_phone_two)
    TextView tvPhoneTwo;
    @BindView(R.id.tv_delete_two)
    TextView tvDeleteTwo;
    @BindView(R.id.ll_call_two)
    LinearLayout llCallTwo;
    @BindView(R.id.tv_name_three)
    TextView tvNameThree;
    @BindView(R.id.tv_phone_three)
    TextView tvPhoneThree;
    @BindView(R.id.tv_delete_three)
    TextView tvDeleteThree;
    @BindView(R.id.ll_call_three)
    LinearLayout llCallThree;
    @BindView(R.id.tv_add_call)
    TextView tvAddCall;

    private int number;


    @Override
    protected int getLayoutResource() {
        return R.layout.aty_call_person;
    }

    @Override
    protected void initViews() {
        tvTitle.setText( "紧急联系人" );
    }

    @Override
    protected void initData() {
        findContact();
    }


    private void findContact() {
        RestProxy.getInstance().findContact( new Observer<ContactFindVO>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @SuppressLint("SetTextI18n")
            @Override
            public void onNext(ContactFindVO contactFindVO) {
                if (UtilString.isEmpty( contactFindVO.data.contactName1 )) {
                    number = 1;
                    llCallOne.setVisibility( View.GONE );
                } else {
                    llCallOne.setVisibility( View.VISIBLE );
                }
                if (UtilString.isEmpty( contactFindVO.data.contactName2 )) {
                    number = 2;
                    llCallTwo.setVisibility( View.GONE );
                } else {
                    llCallTwo.setVisibility( View.VISIBLE );
                }
                if (UtilString.isEmpty( contactFindVO.data.contactName3 )) {
                    number = 3;
                    llCallThree.setVisibility( View.GONE );
                } else {
                    llCallThree.setVisibility( View.VISIBLE );
                }
                if (!UtilString.isEmpty( contactFindVO.data.contactName1 ) && !UtilString.isEmpty( contactFindVO.data.contactName2 ) && !UtilString.isEmpty( contactFindVO.data.contactName3 )) {
                    tvAddCall.setVisibility( View.GONE );
                }else {
                    tvAddCall.setVisibility( View.VISIBLE );
                }
                tvNameOne.setText( contactFindVO.data.contactName1 );
                tvPhoneOne.setText( contactFindVO.data.contactPhone1 );
                tvNameTwo.setText( contactFindVO.data.contactName2 );
                tvPhoneTwo.setText( contactFindVO.data.contactPhone2 );
                tvNameThree.setText( contactFindVO.data.contactName3 );
                tvPhoneThree.setText( contactFindVO.data.contactPhone3 );
            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
                ToastUtil.onError( mContext, e );
            }

            @Override
            public void onComplete() {

            }
        } );
    }


    @OnClick({R.id.tv_delete_one, R.id.tv_delete_two, R.id.tv_delete_three, R.id.tv_add_call})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_delete_one:
                tvNameOne.setText( "" );
                tvPhoneOne.setText( "" );
                deleteContact();
                break;
            case R.id.tv_delete_two:
                tvNameTwo.setText( "" );
                tvPhoneTwo.setText( "" );
                deleteContact();
                break;
            case R.id.tv_delete_three:
                tvNameThree.setText( "" );
                tvPhoneThree.setText( "" );
                deleteContact();
                break;
            case R.id.tv_add_call:
                showAddCallPersonDialog();
                break;
        }
    }


    private void showAddCallPersonDialog() {
        String contactName1 = tvNameOne.getText().toString();
        String contactPhone1 = tvPhoneOne.getText().toString();
        String contactName2 = tvNameTwo.getText().toString();
        String contactPhone2 = tvPhoneTwo.getText().toString();
        String contactName3 = tvNameThree.getText().toString();
        String contactPhone3 = tvPhoneThree.getText().toString();
        AddCallPersonDialog mAddCallPersonDialog = AddCallPersonDialog.newInstance( number, contactName1, contactPhone1, contactName2, contactPhone2, contactName3, contactPhone3 );
        mAddCallPersonDialog.show( getSupportFragmentManager(), "AddCallPersonDialog" );
        mAddCallPersonDialog.setmListener( new AddCallPersonDialog.OnItemClickListener() {
            @Override
            public void onConfirm() {
                findContact();
            }

            @Override
            public void onCancel() {

            }
        } );
    }

    private void deleteContact() {

        showProgressDialog( mContext );
        Map<String, Object> map = new HashMap<>();
        map.put( "contactName1", tvNameOne.getText().toString() );
        map.put( "contactPhone1", tvPhoneOne.getText().toString() );
        map.put( "contactName2", tvNameTwo.getText().toString() );
        map.put( "contactPhone2", tvPhoneTwo.getText().toString() );
        map.put( "contactName3", tvNameThree.getText().toString() );
        map.put( "contactPhone3", tvPhoneThree.getText().toString() );

        RestProxy.getInstance().addContact( map, new Observer<BaseResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(BaseResponse baseResponse) {
                findContact();
            }

            @Override
            public void onError(Throwable e) {
                ToastUtil.onError( mContext, e );
                dismissProgressDialog();
            }

            @Override
            public void onComplete() {
                dismissProgressDialog();
            }
        } );


    }

}
